function [FID_Grid,Action,Age,AgeState,States,pproc,DS] = prepdataM10(opt)

% Load data

load([opt.DataFolder opt.MicroData])

% Clean nonconforming values for transportation cost:
DS = DS(double(DS(:,opt.TransCostVar)) ~= -9999,:);

% Sample selection:
% Select random subsample of fields:
if opt.SelSubSample
    [DS] = subsample(DS,opt.SubSampleFrac);
end

if opt.Buffer40km
    [DS] = DS(DS.Buffer40km == 1,:);
end

if opt.SPonly
    [DS] = DS(DS.CanasatDataset == 1,:);
end

if opt.NotPantanal
    [DS] = DS(DS.ECO_NUM ~= 7,:);
end

if opt.NotUcsIndian
    [DS] = DS(DS.indian == 0 & DS.ucs == 0,:);
end

% Eliminate fields in BA and ES:
state = floor(DS.munic*10^-5);
DS = DS(state ~= 29 & state ~= 32,:);

% Eliminate urban and lake:
DS = DS(DS.Urban == 0 & DS.Lake == 0, :);

%% Create action variable
% Get field ages:
[Age] = FieldAge(DS);

% Compute Action or decision variables based on observed field specific
% state change.

% Action:
Action = nan(size(Age));
AgeState = nan(size(Age));

% Actions for fields not in sugarcane:
% Keep out: (Action = 0)
Nper = size(Action,2);
for t=2:Nper
    
    % Actions for fields not in sugarcane:
    % Keep out: (Action = 0)
    Action(Age(:,t) == 0 & Age(:,t-1) == 0,t) = 0;
    % Plant sugarcane: (Action = 1)
    Action(Age(:,t) == 1 & Age(:,t-1) == 0,t) = 1;
    
    % Actions for sugarcane fields:
    % Keep field: (Action = 0)
    Action(Age(:,t) > 1 & (Age(:,t)-Age(:,t-1) == 1),t) = 0;
    % Replant field: (Action = 1)
    Action(Age(:,t) == 1 & Age(:,t-1) > 0,t) = 1;
    % Abandon sugarcane: (Action = -1)
    Action(Age(:,t) == 0 & Age(:,t-1) > 0,t) = -1;
    
    % Age state that corresponds 
    AgeState(:,t) = Age(:,t-1);
    
end

% AgeState goes from 1 to opt.MaxAge:
AgeState = AgeState + 1;

% Truncate at very old age:
if opt.TrunkAge
    AgeState( any(AgeState >= opt.TrunkAgeLevel,2), : ) = NaN;
    if opt.ElimFrac
        RandFields = rand(size(AgeState,1),1);
        ElimFrac = ((any(AgeState >= 5,2) & ~(any(Action==1,2) | any(Action==-1,2))) & (RandFields < 0.4));
        AgeState( ElimFrac, : ) = NaN;
    end
end

% Cap Age  State according to options:
AgeState(AgeState >= opt.MaxAge) = opt.MaxAge;

% Generate and classify data in states. Also run AR model for outside
% option return 
PriceChange.on = 0; % Set to off (this is ON only for counterfactual)
[States,pproc] = GenStateVariables(DS,opt,AgeState,PriceChange);

% Some fields will have no classification in terms of outside option.
% Eliminate those fields below:
Fields2keep = (States.RegionsForExoStates > 0);

States.ExoTimeStates  = States.ExoTimeStates(Fields2keep,:);
States.ExoFixedStates = States.ExoFixedStates(Fields2keep,:);
States.SingleStateIdx = States.SingleStateIdx(Fields2keep,:);
States.RegionsForExoStates = States.RegionsForExoStates(Fields2keep > 0);
DS                    = DS(Fields2keep > 0,:);
AgeState              = AgeState(Fields2keep,:);
Action                = Action(Fields2keep,:);
Age                   = Age(Fields2keep,:);

AgeState              = int8(AgeState);
Action                = int8(Action);
Age                   = int8(Age);
%FID_Grid = DS.FID_Grid;
FID_Grid = uint32(DS.FID_Grid);


